﻿using System.Web.Mvc;
using SlamCms.Data;
using SlamCms.Web;
using SlamCmsDemo.Data;
using SlamCmsDemo.Web.Core;
using SlamCmsDemo.Web.Core.Models;

namespace SlamCmsDemo.HtmlCss.Controllers
{
	public class CalendarController : Controller
	{
		private readonly SlamCmsContext slamCmsContext;

		public CalendarController(SlamCmsContext slamCmsContext)
		{
			this.slamCmsContext = slamCmsContext;
		}

		[HttpGet]
		public ActionResult Index()
		{
			var events = slamCmsContext.CreateQuery(QueryBuilderOptions.AllZones)
									   .FilterContentTypes<Event>()
									   .Get<Event>();

			var model = new CalendarIndexViewModel();
			model.Events = events;

			ViewData.Model = model;
			return View();
		}

		[HttpGet]
		public ActionResult Add(string eventId)
		{
			var @event = slamCmsContext.CreateQuery(QueryBuilderOptions.AllZones)
									   .FilterContentItemId(eventId)
									   .GetFirstOrDefault<Event>();

			if (@event == null)
				return this.NotFound();

			return new CalendarResult(@event);
		}

		[HttpGet]
		public ActionResult List(TagBrowserParameters parameters)
		{
			var events = slamCmsContext.CreateQuery(QueryBuilderOptions.AllZones)
									   .IncludeTags()
									   .FilterContentTypes<Event>()
									   .FilterTaggedWithAll(parameters.FilteredTags)
									   .Get<Event>();

			var tagTree = slamCmsContext.GetTagTree()
										.RemoveHiddenTagsForTagMenu()
										.ForTagBrowser(this, parameters)
										.UpdateContentItemCount(events)
										.RemoveTagsWithNoItems()
										.RemoveTagCategoriesWithNoTags();

			var model = new CalendarListViewModel();
			model.TagTree = tagTree;
			model.Items = events.ToContentList("Featured Events");

			ViewData.Model = model;
			return View();
		}
	}
}
